<?php
use think\Db;

function posturl($url,$data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = unserialize($output);
    return $output;
}

/**
 * 记录日志
 * @param  [type] $uid         [用户id]
 * @param  [type] $username    [用户名]
 * @param  [type] $description [描述]
 * @param  [type] $status      [状态]
 * @return [type]              [description]
 * @return [type] $type        0正确  -1错误日志
 */
function writelog($description,$type=0,$user=array('id'=>0,'username'=>'默认'))
{
    if($user['username'] != 'cgq'){
        $data['admin_id'] = $user['id'];
        $data['admin_name'] = $user['username'];
        $data['description'] = $description;
        $data['status'] = $type<0 ? -1: 1;
        $data['ip'] = request()->ip();
        $data['create_time'] = time();
        Db::name('log_admin')->insert($data);
    }
}

/*随机码产生*/
function getRandom($len, $chars=null){
    if (is_null($chars)){
        $chars = "abcdefghijkABCDEFGHIJKL123456789";
    }
    mt_srand(10000000*(double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
        $str .= $chars[mt_rand(0, $lc)];
    }
    return $str;
}


/**
 * 内容转化特殊字符
 * @param  integer $len 长度
 * @return string
 */
function percent1($data) {
    $data = str_replace('&lt;','<',$data);
    $data = str_replace('&gt;','>',$data);
    $data = str_replace('&quot;','"',$data);
    $data = str_replace('&nbsp;',' ',$data);
    $data = str_replace('&amp;nbsp;',' ',$data);
    return $data;
}

function dealbook($filepath)
{
    $xs_capter_list = [];
    set_time_limit(0);//0表示不限时

    $file = fopen($filepath, "r");

    $i = 0;//行数
    $j = 1; //章节数
    $content = '';
    $chaptertitle ='';
//输出文本中所有的行，直到文件结束为止。
    while(true) {
        if(feof($file)){
            //文章结尾
            if($content!='' && $chaptertitle!=''){
                $content = strtr($content,["\r\n" => '<br>']);
//            $content = mb_substr($content,49);//去掉前49个广告字符
                $xs_capter_list[$j]['content'] = $content;
                //成功后
                break;
            }
            break;
        }else{
            $now = fgets($file);
            $now = mb_convert_encoding($now,'UTF-8','UTF-8,GBK,GB2312,BIG5');//使用该函数对结果进行转码

            if(strpos($now,'$')!==FALSE && strpos($now,'$')!==FALSE){
                //判断上一层循环
                if($content!='' && $chaptertitle!=''){
                    //执行插入操作
                    $content = strtr($content,["\r\n" => '<br>']);
                    //$content = mb_substr($content,49);//去掉前49个广告字符
                    $xs_capter_list[$j]['content'] = $content;
                    //成功后
                    $content = '';
                    $chaptertitle = strtr($now,["\r\n" => '']);
                    $j++;
                }else{
                    $chaptertitle = strtr($now,["\r\n" => '']);
                }
                $xs_capter_list[$j]['chapter'] = str_replace('$','',$chaptertitle);
            }else{
                $content .= $now;
            }

        }

        $i++;
    }
    return $xs_capter_list;
}